package com.kgc.fk.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.collection.pojo.BuyPay;
import com.kgc.collection.pojo.BuyPayDetail;
import com.kgc.fk.dao.BuyPayDao;
import com.kgc.fk.service.BuyPayService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
public class BuyPayServiceImpl implements BuyPayService {

    private Logger logger = LoggerFactory.getLogger(BuyPayServiceImpl.class);

    @Resource
    private BuyPayDao buyPayDao;

    @Override
    public ResponseMsg getBuyPayPage(Map<String, Object> map) {
        logger.info("BuyPayServiceImpl getBuyPayPage params{}" ,map);
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(500);
        int pageNum = 0;
        int pageSize = 8;
        if(map.get("pageNum") instanceof String){
            pageNum =Integer.parseInt((String) map.get("pageNum"));
            pageSize =Integer.parseInt((String) map.get("pageSize"));
        }else {
            pageNum = (Integer) map.get("pageNum");
            pageSize = (Integer) map.get("pageSize");
        }
        PageHelper.startPage(pageNum, pageSize);
        List<BuyPay> payList = buyPayDao.getPayList(map);
        PageInfo<BuyPay> pageInfo = new PageInfo<>(payList);
        responseMsg.setData(pageInfo);
        responseMsg.setCode(200);
        logger.info("BuyPayServiceImpl getBuyPayPage end params{} response{}" ,map , responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg updateBuyPay(Map<String, Object> map) {
        logger.info("BuyPayServiceImpl updateBuyPay params{}" ,map);
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(500);
        int result = buyPayDao.updatePay(map);
        if(result < 0){
            return responseMsg;
        }
        responseMsg.setCode(200);
        logger.info("BuyPayServiceImpl updateBuyPay end params{} response{}" ,map , responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg addBuyPay(Map<String, Object> map) {
        logger.info("addBuyPay map = " + map);
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(500);
        Date date = new Date();
        map.put("createDate", date);
        int result = buyPayDao.addBuyPay(map);
        if(result < 0){
            responseMsg.setMsg("添加主订单失败");
            return responseMsg;
        }
        ArrayList<Map<String, Object>> infoList =(ArrayList<Map<String, Object>>) map.get("list");
        for (Map<String, Object> info : infoList) {
            info.put("majorCode", map.get("code"));
            info.put("createDate",date);
            info.put("createId",info.get("createId"));
            result = buyPayDao.addBuyPayDetail(info);
            if(result < 0){
                responseMsg.setMsg("添加详情失败");
                return responseMsg;
            }
        }
        responseMsg.setCode(200);
        logger.info("addBuyPay end params{} response{}" ,map , responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg getBuyPayByCode(Map<String, Object> map) {
        logger.info("BuyPayServiceImpl getBuyPayByCode params{}" ,map);
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(500);
        List<BuyPay> payList = buyPayDao.getPayList(map);
        logger.info("getBuyPay map{} payList{}",map,payList);
        for (BuyPay buyPay : payList) {
            List<BuyPayDetail> list = buyPayDao.getBuyPayDetail(buyPay.getCode());
            buyPay.setList(list);
        }
        logger.info("getBuyPayDetailByCode map{} payList{}",map,payList);
        responseMsg.setCode(200);
        responseMsg.setData(payList.get(0));
        logger.info("getBuyPayDetailByCode end params{} response{}" ,map , responseMsg);
        return responseMsg;
    }

    @Override
    public ResponseMsg modifyBuyPay(Map<String, Object> map) {
        logger.info("modifyBuyPay map {} " , map);
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setCode(500);
        String code = (String) map.get("code");
        ArrayList<Map<String, Object>> infoList =(ArrayList<Map<String, Object>>) map.get("list");
        Date date = new Date();
        map.put("modiDate", date);
        int result = buyPayDao.modiBuyPay(map);

        logger.info("modiBuyPay params{} result{}" ,map, result);
        result = buyPayDao.delBuyPayDetail(code);
        logger.info("delBuyPayDetail params{} result{}" ,infoList, result);
        if (result <= 0) {
            return responseMsg;
        }
        for (Map<String, Object> info : infoList) {
            info.put("majorCode", code);
            info.put("createDate", date);
            info.put("modiId", map.get("modiId"));
            result = buyPayDao.addBuyPayDetail(info);
            if (result <= 0) {
                return responseMsg;
            }
        }
        logger.info("modiBuyPay params{} result{}" ,infoList, result);
        responseMsg.setCode(200);
        logger.info("modifyBuyPay end params{} response{}" ,map , responseMsg);
        return responseMsg;


    }
}
